package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.WinNum;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.WinNumInfo;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.ItemService;
import ysq.xyg.service.WinNumService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
/***
 * @descroption
 * @param  * @param null
 * @return
 * @author shengQiang yu
 * @date 2018/8/10 8:22
 */
@Controller
@RequestMapping("/winNum")
public class WinNumController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(WinNumController.class);
    @Resource
    private WinNumService winNumService;
    @Resource
    private ItemService itemService;

    private WinNum winNum;

    @RequestMapping("/listPage")
    public ModelAndView list(String activityId){
        ModelAndView view = new ModelAndView("/winNumList");
        view.addObject("activityId", activityId);
        return view;
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(WinNum winNum, Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<WinNum> pageInfo = winNumService.pageInfoLeft(winNum, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (WinNum item : pageInfo.getList()) {
                row=new JSONObject();
                row.put("id", item.getId());
                row.put("num", item.getNum());
                if (StringUtil.isNotEmpty(item.getNickName())) {
                    row.put("nickName", BaseUtil.decodeBase64(item.getNickName()));
                }
                row.put("state", item.getState());
                row.put("phone", item.getPhone());
                row.put("createTime", DateUtil.DateToStr(item.getCreateTime(), DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id, String itemId){
        ModelAndView view = new ModelAndView("/WinNumUpdateOrSave");
        winNum = winNumService.selectByPrimaryKey(id);
        view.addObject("winNum", winNum);
        return view;
    }

    /**
     * 设置默认中奖号
     */
    @RequestMapping(value = "/defaultWinningNum",method = RequestMethod.GET)
    public @ResponseBody JSONObject defaultWinningNum(String activityId, String winningNum){
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(activityId)) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        } else {
            int ids = winNumService.defaultWinningNum(activityId, winningNum);
            if (ids <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
